import 'package:dio/dio.dart';
import 'package:jyofservice/util/webconfig.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:jyofservice/util/AesUtil.dart';
import 'package:shared_preferences/shared_preferences.dart';

class UpdatePwdPage extends StatefulWidget {
  @override
  _UpdatePwdPageState createState() => _UpdatePwdPageState();
}

class _UpdatePwdPageState extends State<UpdatePwdPage> {
  bool isloading = true;
  late SharedPreferences sharedPreferences;
  String? phone = "";
  String? pwd = "";
  TextEditingController oldpwd = TextEditingController();
  TextEditingController newpwd = TextEditingController();
  TextEditingController repeatpwd = TextEditingController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    init();
  }

  Future init() async {
    sharedPreferences = await SharedPreferences.getInstance();
    phone = sharedPreferences.getString("username");
    pwd = sharedPreferences.getString("pwd");
    if (mounted) {
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        title: Text(
          '修改密码',
          style:
              TextStyle(color: Colors.white, fontSize: ScreenUtil().setSp(40)),
        ),
        centerTitle: true,
      ),
      body: Stack(
        children: <Widget>[
          SingleChildScrollView(
              child: Container(
            width: ScreenUtil().screenWidth,
            height: ScreenUtil().screenHeight,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                SizedBox(
                  height: ScreenUtil().setHeight(100),
                ),
                Text(
                  '正在为' + phone! + "账号修改密码",
                  style: TextStyle(
                      color: Colors.black87, fontSize: ScreenUtil().setSp(38)),
                ),
                SizedBox(
                  height: ScreenUtil().setHeight(100),
                ),
                Container(
                    width: ScreenUtil().setWidth(600),
                    height: ScreenUtil().setHeight(100),
                    child: TextField(
                      controller: oldpwd,
                      //光标颜色属性, 绘制光标是使用的颜色
                      cursorColor: Colors.blue,
                      obscureText: false,
                      cursorWidth: 2.0,
                      style: TextStyle(
                        fontSize: ScreenUtil().setSp(40),
                        //字母间距
                        letterSpacing: 0.5,
                        color: const Color(0xFF4A4A4A),
                      ),
                      //装饰属性, 在文本字段周围显示的装饰
                      //输入装饰组件
                      decoration: InputDecoration(
                        contentPadding: EdgeInsets.only(left: 5.0),
                        border: InputBorder.none,
                        filled: true,
                        fillColor: const Color(0xFFFFFFFF),
                        // 是密集属性，输入子项是否是密集形式的一部分（即使用较少的垂直空间）。
                        isDense: true,
                        hintStyle: TextStyle(
                          fontSize: 16.0,
                          color: Color(0xFF9B9B9B),
                        ),
                        hintText: "请输入当前密码",
                        //  prefixIcon: widget.prefixIcon,
                        //清除按钮
                        suffix: IconButton(
                            icon: Icon(
                              Icons.clear,
                              color: Colors.black45,
                            ),
                            onPressed: () {
                              oldpwd.clear();
                            }),
                      ),
                    )),
                Container(
                  width: ScreenUtil().setWidth(600),
                  height: ScreenUtil().setHeight(2),
                  color: Colors.black12,
                ),
                Container(
                    width: ScreenUtil().setWidth(600),
                    height: ScreenUtil().setHeight(100),
                    child: TextField(
                      controller: newpwd,
                      //光标颜色属性, 绘制光标是使用的颜色
                      cursorColor: Colors.blue,
                      obscureText: true,
                      cursorWidth: 2.0,
                      style: TextStyle(
                        fontSize: ScreenUtil().setSp(40),
                        //字母间距
                        letterSpacing: 0.5,
                        color: const Color(0xFF4A4A4A),
                      ),
                      //装饰属性, 在文本字段周围显示的装饰
                      //输入装饰组件
                      decoration: InputDecoration(
                        contentPadding: EdgeInsets.only(left: 5.0),
                        border: InputBorder.none,
                        filled: true,
                        fillColor: const Color(0xFFFFFFFF),
                        // 是密集属性，输入子项是否是密集形式的一部分（即使用较少的垂直空间）。
                        isDense: true,
                        hintStyle: TextStyle(
                          fontSize: 16.0,
                          color: Color(0xFF9B9B9B),
                        ),
                        hintText: "请输入新密码",
                        //  prefixIcon: widget.prefixIcon,
                        //清除按钮
                        suffix: IconButton(
                            icon: Icon(
                              Icons.clear,
                              color: Colors.black45,
                            ),
                            onPressed: () {
                              newpwd.clear();
                            }),
                      ),
                    )),
                Container(
                  width: ScreenUtil().setWidth(600),
                  height: ScreenUtil().setHeight(2),
                  color: Colors.black12,
                ),
                Container(
                    width: ScreenUtil().setWidth(600),
                    height: ScreenUtil().setHeight(100),
                    child: TextField(
                      controller: repeatpwd,
                      //光标颜色属性, 绘制光标是使用的颜色
                      cursorColor: Colors.blue,
                      obscureText: true,
                      cursorWidth: 2.0,
                      style: TextStyle(
                        fontSize: ScreenUtil().setSp(40),
                        //字母间距
                        letterSpacing: 0.5,
                        color: const Color(0xFF4A4A4A),
                      ),
                      //装饰属性, 在文本字段周围显示的装饰
                      //输入装饰组件
                      decoration: InputDecoration(
                        contentPadding: EdgeInsets.only(left: 5.0),
                        border: InputBorder.none,
                        filled: true,
                        fillColor: const Color(0xFFFFFFFF),
                        // 是密集属性，输入子项是否是密集形式的一部分（即使用较少的垂直空间）。
                        isDense: true,
                        hintStyle: TextStyle(
                          fontSize: 16.0,
                          color: Color(0xFF9B9B9B),
                        ),
                        hintText: "确认新密码",
                        //  prefixIcon: widget.prefixIcon,
                        //清除按钮
                        suffix: IconButton(
                            icon: Icon(
                              Icons.clear,
                              color: Colors.black45,
                            ),
                            onPressed: () {
                              repeatpwd.clear();
                            }),
                      ),
                    )),
                Container(
                  width: ScreenUtil().setWidth(600),
                  height: ScreenUtil().setHeight(2),
                  color: Colors.black12,
                ),
                SizedBox(
                  height: ScreenUtil().setHeight(100),
                ),
                SizedBox(
                  width: ScreenUtil().setWidth(600),
                  child: new Card(
                    shape: const RoundedRectangleBorder(
                        borderRadius: BorderRadius.all(Radius.circular(50.0))),
                    color: Colors.blue,
                    elevation: 10.0,
                    child: new TextButton(
                        onPressed: () {
                          if(newpwd.text.toString().length==0) {
                            Fluttertoast.showToast(msg: "新密码不能为空");
                            return;
                          }
                          if (newpwd.text.toString() !=
                              repeatpwd.text.toString()) {
                            Fluttertoast.showToast(msg: "新密码不一致请重新输入");
                            return;
                          }
                          updatepwd();
                        },
                        child: new Padding(
                          padding: new EdgeInsets.all(10.0),
                          child: Text(
                            '确认修改',
                            textAlign: TextAlign.center,
                            style: TextStyle(
                              fontSize: 20.0,
                              letterSpacing: 26,
                              color: Colors.white,
                            ),
                          ),
                        )),
                  ),
                )
              ],
            ),
          )),
          Offstage(
            offstage: isloading,
            child: SpinKitFadingCircle(
              color: Colors.blue,
              size: 50.0,
            ),
          )
        ],
      ),
    );
  }

  Future updatepwd() async {
    if (mounted) {
      setState(() {
        isloading = false;
      });
    }
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    String? key = sharedPreferences.getString("key");
    String aesData = await AesUtil.encodeBase64();
    var data = {
      "key": key,
      "ClientType": "phone",
      "ClientKey": aesData,
      "password1": oldpwd.text.toString(),
      "password2": newpwd.text.toString(),
      "password3": repeatpwd.text.toString()
    };
    Response response = await Dio().post(
        WebConfig().baseUrl + "DB/updatemyselfpassword",
        data: data);
    print(response.data);

    if(response.data["Code"]=="200") {
      Fluttertoast.showToast(msg: "修改成功");
      Navigator.pop(context);
    }else{
      Fluttertoast.showToast(msg: response.data["Msg"]);
    }
    if (mounted) {
      setState(() {
        isloading = true;
      });
    }
    return response.data;
  }
}
